# -*- coding:utf-8 -*-

"""
=============================
this is a feibot product,
all rights reserved.
www.feibot.com
@author: Gu Tianyuan
@email: g@feibot.com
@time: 2023/3/9:10:47
@product: PyCharm
=============================
"""
import psutil
from threading import Thread


def getRunInfo():
    cpuPercent = psutil.cpu_percent(True)
    ramPercent = psutil.virtual_memory()[2]
    ps = psutil.pids()
    psNum = len(ps)
    thrNum = 0
    cpuTemp = 0
    try:
        for p in ps:
            pObj = psutil.Process(p)
            thrNum += pObj.num_threads()
        temps = psutil.sensors_temperatures()

        if temps:
            for name in temps:
                for entry in temps[name]:
                    if entry.current:
                        cpuTemp = round(entry.current, 2)
                        break
                if cpuTemp is not None:
                    break
    except Exception as e:
        print(e)
    return [cpuPercent, ramPercent, psNum, thrNum, cpuTemp]


def getUptime(seconds):
    h = divmod(seconds, 3600)[0]
    m = divmod(seconds % 3600, 60)[0]
    s = seconds % 60
    uptime = ''
    if h > 0:
        uptime += str(h) + "h"
    if m > 0:
        uptime += str(m) + "m"
    uptime += str(s) + "s"
    return uptime


if __name__ == '__main__':
    getRunInfo()
